package binarytree;

/**
 * @Author: 海琳琦
 * @Date: 2022/2/19 23:55
 * https://leetcode-cn.com/problems/count-complete-tree-nodes/
 * 给你一棵 完全二叉树 的根节点 root ，求出该树的节点个数。
 */
public class CountNodes {

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode() {
        }

        public TreeNode(int val) {
            this.val = val;
        }

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }


    public int countNodes(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = countNodes(root.left);
        int right = countNodes(root.right);
        int middle = left + right + 1;
        return middle;
    }

    public static void main(String[] args) {

    }
}
